package com.example.prjtimerservice;
import android.app.Service;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {
    public static final String NOTIFICATION = "com.vogella.android.service.receiver";
    public static final String SECOND = "SECOND";
    

    private void publishResults(long second) {
        Intent intent = new Intent(NOTIFICATION);
        intent.putExtra(SECOND, Long.toString(second));
        sendBroadcast(intent);
        System.out.println("Broadcast: " + second + "/sec");
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
        showTime();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void showTime() {
        long time= (1 * 10 * 1000);
        new CountDownTimer(time, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                publishResults(millisUntilFinished / 1000);
            }

            @Override
            public void onFinish() {
                //Toast.makeText(getApplicationContext(), "Hello World", Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "倒數計時完成", Toast.LENGTH_LONG).show();
                publishResults(0);
                stopSelf();
            }
        }.start();

    }
}
